//
//  MHHYNavigationVC.swift
//  YueChat
//
//  Created by IMac on 2024/9/10.
//

import UIKit

class YLNavigationVC: UINavigationController {

    override func viewDidLoad() {
            super.viewDidLoad()
            
            // iOS 13 及以上版本使用新的 Appearance API
            if #available(iOS 13.0, *) {
                
                // 设置状态栏颜色
                 let tabbarAppearance = UITabBarAppearance()
                 tabbarAppearance.configureWithOpaqueBackground()
                 tabbarAppearance.backgroundColor = .white
                 UITabBar.appearance().standardAppearance = tabbarAppearance
                 
                 // 设置导航栏颜色
                 let navibarAppearance = UINavigationBarAppearance()
                 navibarAppearance.configureWithOpaqueBackground()
                navibarAppearance.backgroundColor = kGrayBG
                
                
                navibarAppearance.shadowImage = UIImage()
                navibarAppearance.shadowColor = .clear
                
                navibarAppearance.titleTextAttributes = [
                    .foregroundColor: UIColor(white: 0, alpha: 1), // 假设这是你定义的颜色
                    .font: UIFont.systemFont(ofSize: 18, weight: .medium)
                ]
                
                 UINavigationBar.appearance().standardAppearance = navibarAppearance
                 UINavigationBar.appearance().scrollEdgeAppearance = navibarAppearance

//                let appearance = UINavigationBarAppearance()
//                appearance.backgroundImage = UIImage(named: "home_navbg")
//                appearance.backgroundColor = .white // 假设 FontManage.MHWhiteColor 是白色
//                appearance.backgroundEffect = nil
//                appearance.shadowImage = UIImage()
//                appearance.shadowColor = .clear
//                appearance.titleTextAttributes = [
//                    .foregroundColor: UIColor(white: 0, alpha: 1), // 假设这是你定义的颜色
//                    .font: UIFont.systemFont(ofSize: 18, weight: .bold)
//                ]
//                
//                navigationController?.navigationBar.standardAppearance = appearance
//                navigationController?.navigationBar.scrollEdgeAppearance = appearance
            } else {
                // iOS 12 及以下版本使用旧的方法设置样式
                navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
            }
        }

        // 重写 pushViewController 方法以添加自定义行为
    @objc override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if children.count >= 1 {
                viewController.hidesBottomBarWhenPushed = true
                let backImage = UIImage(named: "Back")
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(image: backImage, style: .plain, target: self, action: #selector(leftBarButtonItemPop))
            }
            super.pushViewController(viewController, animated: animated)
        }

        // 定义一个方法来处理左BarButtonItem的动作
        @objc func leftBarButtonItemPop() {
            popViewController(animated: true)
        }
    }

    // 假设 FontManage 是你的颜色管理类
    extension UIColor {
        static let MHWhiteColor = UIColor.white
        static let MHBlockColor = UIColor.black // 这里用黑色代替，根据实际情况替换
    }
